package com.liang.response;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.liang.response.core.Director;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.Properties;
import java.util.Scanner;

/**
 * @author liangyehao
 * @version 1.0
 * @date 2021/4/27 23:02
 * @content
 */
public class Main {

    public static void main(String[] args){
        System.out.println("======================================================");
        System.out.println("                   * 23秒响应 *                        ");
        System.out.println("======================================================");
        System.out.println("(C) 2021 Liang Yehao，All rights reserved.");
        System.out.println("版权所有，侵权必究.");
        try {
            if (isExpired()) {
                System.out.println("体验版本已到期，请联系开发者：13119583082");
                System.out.println();
                exit();
            }else{
                printInstructions();
                process();
            }
        } catch (Exception e) {
            System.out.println("程序异常，请联系开发者");
            e.printStackTrace();
        }
    }

    private static void printInstructions() {
        System.out.println("请先将要处理的底表数据复制到excel目录下的response-basic.xlsx中（必须保留表头），按任意键继续...");
        Scanner sc = new Scanner(System.in);
        sc.nextLine();
    }

    public static void process() throws FileNotFoundException {
        String filePath = System.getProperty("user.dir")+File.separator+"excel"+File.separator;
        String basicFileName = filePath+"response-basic.xlsx";
        File basicFile = new File(basicFileName);
        if (!basicFile.exists()) {
            System.out.println(basicFileName+"不存在，请将其放在程序同级目下重试....");
            exit();
        }
        System.out.println("统计区间：开始时间<=    <=结束时间");
        System.out.println("请输入开始时间：（如：2021-01-01）");
        Scanner startTimeScanner = new Scanner(System.in);
        String startTimeStr = startTimeScanner.nextLine();
        System.out.println("请输入结束时间：（如：2021-01-02）");
        Scanner endTimeScanner = new Scanner(System.in);
        String endTimeStr = endTimeScanner.nextLine();

        String fileName = Director.doWork(filePath, basicFile, startTimeStr, endTimeStr);

        System.out.println();

        System.out.println("表格处理完成："+fileName);
        exit();
    }

    public static void exit(){
        System.out.println("输入任意键退出程序....");
        Scanner exitScanner = new Scanner(System.in);
        exitScanner.nextLine();
        System.exit(0);
    }

    public static Date getWebTime() throws IOException {
        String webUrl = "http://www.baidu.com";
        URL url=new URL(webUrl);
        URLConnection conn=url.openConnection();
        conn.connect();
        long dateL=conn.getDate();
        return new Date(dateL);
    }

    public static boolean isExpired() throws Exception {
        Properties properties = new Properties();
        InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("config.properties");
        properties.load(inputStream);
        String expired = properties.get("expired").toString();
        System.out.println("当前为体验版本");
        System.out.println("过期时间【"+expired+"】");
        System.out.println("【contact：13119583082】");
        System.out.println("=====================================================");
        System.out.println();
        DateTime expiredDateTime = DateUtil.parse(expired);
        return expiredDateTime.isBefore(new Date());
    }

}
